package cn.com.controller.administrator;

import cn.com.dao.ClaDao;
import cn.com.entity.Clazz;
import cn.com.entity.Course;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>Project:Academic - ClaController
 *
 * @author 黄俊玮 On Administrator
 * @version 1.0
 * @DataTime 2024/6/14 08:50:19
 * @since 1.8
 */
public class ClaController {
    private ClaDao claDao = new ClaDao();

    public void courseManager(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        List<Course> course = claDao.findCourse();
        request.setAttribute("course",course);
        request.getRequestDispatcher("/WEB-INF/jsp/manager/class/courseManagement.jsp").forward(request,response);
    }
    public void updateCourseName(HttpServletRequest request,HttpServletResponse response) throws  IOException , ServletException{
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        request.setAttribute("id",id);
        request.setAttribute("name",name);
        request.getSession().setAttribute("id",id);
        request.getRequestDispatcher("/WEB-INF/jsp/manager/class/updateCourses.jsp").forward(request,response);
    }
    public void modifyCourseNow(HttpServletRequest request,HttpServletResponse response) throws  IOException , ServletException{
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String oid = (String) request.getSession().getAttribute("id");

        boolean key = claDao.updateCourse(id,name,oid);

        if (key){
            response.sendRedirect("/Administrator/courses.do");
        }else {
            request.setAttribute("err","<span style=\"color:red\">课程编号已存在</span>");
            request.getRequestDispatcher("/Administrator/courses.do").forward(request,response);
        }

    }
    public void classManager(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        List<Clazz> list = claDao.find();
        request.setAttribute("findClass",list);
        request.getRequestDispatcher("/WEB-INF/jsp/manager/class/classManagement.jsp").forward(request, response);
    }
    public void update(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException{
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        req.setAttribute("id",id);
        req.setAttribute("name",name);
        req.getSession().setAttribute("oid",id);
        req.getRequestDispatcher("/WEB-INF/jsp/manager/class/updateName.jsp").forward(req,resp);
    }
    public void modifyClassNow(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException{
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String oid = (String) req.getSession().getAttribute("oid");
        claDao.updateClassName(id,name,oid);
        req.getRequestDispatcher("/Administrator/classManager.do").forward(req,resp);
    }
    public void deleteClass(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException{
        String id = req.getParameter("id");
        claDao.delete(id);
        req.getRequestDispatcher("/Administrator/classManager.do").forward(req,resp);
    }
    public void addClass(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException{
        req.getRequestDispatcher("/WEB-INF/jsp/manager/class/addClass.jsp").forward(req,resp);
    }
    public void modifyAddClassNow(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException{
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        Boolean b = claDao.addClass(id, name);
        if (b){
            req.getRequestDispatcher("/Administrator/classManager.do").forward(req,resp);
        }else {
            req.setAttribute("err" , "<span style=\"color:red\">班级编号已存在</span>");
            req.getRequestDispatcher("/Administrator/classManager.do").forward(req,resp);
        }
    }
    public void addCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException{
        req.getRequestDispatcher("/WEB-INF/jsp/manager/class/addCourse.jsp").forward(req,resp);
    }

    public void modifyAddCourseNow(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException{
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");

        boolean key = claDao.addCourse(id,name);

        if (key){
            req.getRequestDispatcher("/Administrator/courses.do").forward(req,resp);
        }else {
            req.setAttribute("err" , "<span style=\"color:red\">班级编号已存在</span>");
            req.getRequestDispatcher("/Administrator/courses.do").forward(req,resp);
        }

    }
}
